//
//  TL_JumpManager.m
//  WXReader
//
//  Created by TSL on 2021/10/18.
//  Copyright © 2021 Andrew. All rights reserved.
//

#import "TL_JumpManager.h"

#import <MessageUI/MessageUI.h>


@implementation TL_JumpManager

+ (void)jumpActionWithType:(NSString *)skipType
                   content:(NSString * _Nullable)skipContent
            otherParameter:(NSDictionary * _Nullable)otherParameter
                completion:(void (^ _Nullable) (BOOL isSuccess, NSString *skipType, id _Nullable object))completion {
    if (kObjectIsEmpty(skipType)) return;
    
    UIViewController *currentViewController = [TL_ViewHelper getCurrentViewController];
    UINavigationController *currentNavigationController = currentViewController.navigationController;
    NSString *navTitle = otherParameter[@"title"];
    // navTitle 不能是空字符串(@"")，因为视图控制器内部只判断了标题是否为nil。
    if (kObjectIsEmpty(navTitle)) {
        navTitle = nil;
    }
    
    /*------------------------------ 跳转类型事件 ------------------------------*/
    
    // 跳转小说详情首页
    if ([skipType isEqualToString:Jump_bookDetail]) {

        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转漫画详情首页
    if ([skipType isEqualToString:Jump_comicDetail]) {
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转内部浏览器
    if ([skipType isEqualToString:Jump_internalWebview]) {
        TL_WebViewViewController *webViewController = [[TL_WebViewViewController alloc] init];
        webViewController.navTitle = navTitle ?: @"";
        webViewController.URLString = skipContent;
        [currentNavigationController pushViewController:webViewController animated:YES];
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转外部浏览器
    if ([skipType isEqualToString:Jump_externalWebview]) {
        BOOL canOpenURL = [UIApplication.sharedApplication canOpenURL:[NSURL URLWithString:skipContent]];
        if (canOpenURL) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:skipContent] options:@{} completionHandler:nil];
        }
        !completion ?: completion(canOpenURL, skipType, nil);
        return;
    }
    
    // 跳转小说标签首页
    if ([skipType isEqualToString:Jump_bookLabel]) {
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转小说分类首页
    if ([skipType isEqualToString:Jump_bookCategory]) {
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转小说推荐首页
    if ([skipType isEqualToString:Jump_bookRecommend]) {
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转小说排行榜首页
    if ([skipType isEqualToString:Jump_bookRank]) {
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转免费小说首页
    if ([skipType isEqualToString:Jump_bookFree]) {
       
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转任务中心首页
    if ([skipType isEqualToString:Jump_taskCenter]) {
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转充值中心首页
    if ([skipType isEqualToString:Jump_rechargeCenter]) {
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转会员中心首页
    if ([skipType isEqualToString:Jump_memberCenter]) {
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转帮助反馈首页
    if ([skipType isEqualToString:Jump_feedbackCenter]) {
       
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转设置中心首页
    if ([skipType isEqualToString:Jump_settingCenter]) {
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转月卡中心首页
    if ([skipType isEqualToString:Jump_monthlyCardCenter]) {
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转漫画分类首页
    if ([skipType isEqualToString:Jump_comicCategory]) {
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转漫画推荐首页
    if ([skipType isEqualToString:Jump_comicRecommend]) {
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转漫画排行榜首页
    if ([skipType isEqualToString:Jump_comicRank]) {
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转打赏记录首页
    if ([skipType isEqualToString:Jump_rewardRecordCenter]) {
       
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转阅读历史记录首页
    if ([skipType isEqualToString:Jump_readRecordCenter]) {
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转下载缓存首页
    if ([skipType isEqualToString:Jump_downloadCacheCenter]) {
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转我的评论首页
    if ([skipType isEqualToString:Jump_myCommentCenter]) {
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转联系客服首页
    if ([skipType isEqualToString:Jump_myCustomerServiceCenter]) {
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转注销账号首页
    if ([skipType isEqualToString:Jump_cancelAccountCenter]) {
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转个人信息首页
    if ([skipType isEqualToString:Jump_accountInfoCenter]) {
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转金币明细首页
    if ([skipType isEqualToString:Jump_coinListCenter]) {
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转绑定邮箱首页
    if ([skipType isEqualToString:Jump_bindMailbox]) {
        TL_BindPhoneViewController *bindEmailViewController = [[TL_BindPhoneViewController alloc] init];
        bindEmailViewController.navTitle = navTitle;
        bindEmailViewController.completionBlock = ^{
            !completion ?: completion(YES, skipType, nil);
        };
        [currentNavigationController pushViewController:bindEmailViewController animated:YES];
        return;
    }
    
    
    /*------------------------------ 动作类型事件 ------------------------------*/
    
    // 分享APP
    if ([skipType isEqualToString:Jump_inviteFriends] ||
        [skipType isEqualToString:Jump_shareAPP]) {
//        [TL_ShareManager shareApp];
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 修改昵称
    if ([skipType isEqualToString:Jump_modifyNickname]) {
        TL_TextfieldAlertView *alert = [[TL_TextfieldAlertView alloc] init];
        alert.alertViewTitle = WXLocalizedString(@"修改昵称");
        alert.placeHoldTitle = skipContent ?: WXLocalizedString(@"请输入修改昵称");
        alert.endEditedBlock = ^(NSString *inputText) {
            !completion ?: completion(YES, skipType, inputText);
        };
        [alert showAlertView];
        return;
    }
    
    // 修改性别
    if ([skipType isEqualToString:Jump_modifyGender]) {
        UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//        if (is_iPad) {
//            UIPopoverPresentationController *popover = actionSheet.popoverPresentationController;
//            if (popover) {
//                popover.sourceView = currentViewController.view;
//                popover.sourceRect = currentViewController.view.bounds;
//                popover.permittedArrowDirections = UIPopoverArrowDirectionDown;
//            }
//        }
        
        [actionSheet addAction:[UIAlertAction actionWithTitle:WXLocalizedString(@"男") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            !completion ?: completion(YES, skipType, @"0");
        }]];
        [actionSheet addAction:[UIAlertAction actionWithTitle:WXLocalizedString(@"女") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            !completion ?: completion(YES, skipType, @"1");
        }]];
        [actionSheet addAction:[UIAlertAction actionWithTitle:WXLocalizedString(@"取消") style:UIAlertActionStyleCancel handler:nil]];

        [currentViewController presentViewController:actionSheet animated:YES completion:nil];
        return;
    }
    
    // 绑定Google账号
    if ([skipType isEqualToString:Jump_bindGoogle]) {
        [[TL_GoogleManager sharedManager] tunedUpGoogleWithState:TL_GoogleStateBinding];
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 绑定Facebook账号
    if ([skipType isEqualToString:Jump_bindFacebook]) {
        [[TL_FacebookManager sharedManager] tunedUpFacebookWithState:TL_FacebookStateBinding];
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 跳转到书城首页
    if ([skipType isEqualToString:Jump_collectionBook] ||
        [skipType isEqualToString:Jump_readBook] ||
        [skipType isEqualToString:Jump_reviewBook]) {
        
//        [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Change_Tabbar_Index object:@"1"];
        
        !completion ?: completion(YES, skipType, nil);
        return;
    }
    
    // 领取月卡奖励
    if ([skipType isEqualToString:Jump_getMonthlyCard]) {
//        [TL_NetworkRequestManger POST:Monthly_Card_Reward parameters:@{@"task_id" : skipContent} model:nil success:^(BOOL isSuccess, id  _Nullable t_model, TL_NetworkRequestModel * _Nonnull requestModel) {
//            NSString *msg = [NSString stringWithFormat:@"%@", requestModel.data[@"msg"]];
//            !completion ?: completion(isSuccess, skipType, msg);
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
//            !completion ?: completion(NO, skipType, error);
//        }];
        return;
    }
    
    // 获取客服邮箱
    if ([skipType isEqualToString:Jump_getCustomerServiceEmail]) {
//        NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"message:%@",skipContent]];
//        BOOL canOpenURL = [UIApplication.sharedApplication canOpenURL:phoneURL];
//        if (canOpenURL) {
//            [[UIApplication sharedApplication] openURL:phoneURL options:@{} completionHandler:nil];
//        } else {
//            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
//            pasteboard.string = skipContent ?: @"";
//            [TL_TopAlertManager showAlertWithType:TL_TopAlertTypeSuccess alertTitle:WXLocalizedString(@"已复制到剪切板")];
//        }
//        !completion ?: completion(canOpenURL, skipType, nil);
        return;
    }
    
    // 获取客服电话
    if ([skipType isEqualToString:Jump_getCustomerPhone]) {
        NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",skipContent]];
        BOOL canOpenURL = [UIApplication.sharedApplication canOpenURL:phoneURL];
        if (canOpenURL) {
            [[UIApplication sharedApplication] openURL:phoneURL options:@{} completionHandler:nil];
        }
        !completion ?: completion(canOpenURL, skipType, nil);
        return;
    }
    
    if ([skipType isEqualToString:Jump_FeckBookCustomer]) {
        BOOL canOpenURL = [UIApplication.sharedApplication canOpenURL:[NSURL URLWithString:skipContent]];
        if (canOpenURL) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:skipContent] options:@{} completionHandler:nil];
        }
        !completion ?: completion(canOpenURL, skipType, nil);
        return;
    }
    
    /*------------------------------ 新跳转事件 ------------------------------*/
    
    if ([skipType isEqualToString:@"/noveldetail"]) {
        
        NSString *type = otherParameter[@"type"];
        
        if ([type isEqualToString:@"1"]) {
            //长篇
//            TL_BookMallDetailViewController *vc = [TL_BookMallDetailViewController new];
//            vc.book_id = [otherParameter[@"novelid"] intValue];
//            [[TL_ViewHelper getCurrentViewController].navigationController pushViewController:vc animated:YES];
        }else if ([type isEqualToString:@"10"]){
            //短片小说
            TLShortStoryViewController *vc = [TLShortStoryViewController new];
            vc.worksId = [NSString stringWithFormat:@"%@",otherParameter[@"novelid"]];
            [[TL_ViewHelper getCurrentViewController].navigationController pushViewController:vc animated:YES];
        }
        
    }
    if ([skipType isEqualToString:@"/rechargeVip"]) {
        TLRechargeViewController *vc = [TLRechargeViewController new];
        [[TL_ViewHelper getCurrentViewController].navigationController pushViewController:vc animated:YES];
    }
    if ([skipType isEqualToString:@"/recharge"]) {
        TLRechargeCoinsViewController *vc = [TLRechargeCoinsViewController new];
        [[TL_ViewHelper getCurrentViewController].navigationController pushViewController:vc animated:YES];
    }
    
}

@end
